Assignment Operators
In javascript, the assignment operators are used to assign values to the variables.
Operator | Operator Name |
---|---|
= | assignment operator |
+= | addition assignment operator |
-= | subtraction assignment operator |
*= | multiplication assignment operator |
/= | division assignment operator |
%= | modulo assignment operator |
Assignment operator
The assignment operator is used to assign a value to the variable.
Example
let age = 20;
Here, the value 10 is assigned to the variable age.
Addition Assignment Operator
This operator is used to add a value to a variable value and assigns the result to the variable.
Example
let age = 20;
age+=1;
console.log(age); // 21
The addition assignment operator, adds 1 to the variable age and the value of age becomes 21.
Subtraction Assignment Operator
This operator is used to subtract a value in a variable value and assigns the result to the variable.
Example
let age = 20;
age-=1;
console.log(age); // 19
The subtraction assignment operator, subtracts 1 from the variable age and the value of age becomes 19.
Multiplication Assignment Operator
This operator is used to multiply a value to the variable value and assigns the result to the variable.
Example
let salary = 20000;
salary*=2;
console.log(salary); // 40000
The multiplication assignment operator, multiplies 2 to the variable salary and the value of salary becomes 40000.
Division Assignment Operator
This operator is used to divide a variable value and assign the result to the variable.
Example
let age = 20;
age-=1;
console.log(age); // 19
The subtraction assignment operator, subtracts 1 from the variable age and the value of age becomes 19.
Modulo Assignment Operator
This operator is used to perform the modulo operation to the variable and assigns the result to the variable.
Example
let x = 21;
x%=2;
console.log(x); // 1
When performing modulo of 2 to the variable x it returns and assigns the reminder value to the variable x.